﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Framework;


namespace ODM_AddIn
{
    public class SelectAnalysisAreaButton : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        private ODM_Code.rUtils ru;
        //private ODM_Code.Tools.SelectAnalysisArea selAA;

        public SelectAnalysisAreaButton()
        {
            ru = new ODM_Code.rUtils();
            IMxDocument iDoc = ArcMap.Application.Document as IMxDocument;
            //selAA = new ODM_Code.Tools.SelectAnalysisArea(iDoc);
        }

        protected override void OnClick()
        {
            IMxDocument iDoc = ArcMap.Application.Document as IMxDocument;
            if (ArcMap.Document.FocusMap.LayerCount > 0)
            {
                if (MessageBox.Show("Clear current map?", "Select Analysis Area", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    iDoc.FocusMap.ClearLayers();
                }
            }

            iDoc.ActiveView.Refresh();
            iDoc.ActiveView.ContentsChanged();

            //MessageBox.Show(System.Reflection.Assembly.GetExecutingAssembly().ToString(), "LOCATION");

            ODM_Code.Forms.SelectAnalysisAreaForm saaf = new ODM_Code.Forms.SelectAnalysisAreaForm();
            saaf.ShowDialog();
            saaf.Activate();
            saaf.BringToFront();

            // Reset active tool
            this.resetControl();

            // Update Alternative Selection Box
            
        }

        protected override void OnUpdate()
        {
        }

        protected void resetControl()
        {
            ICommandBars cbrs = ArcMap.Application.Document.CommandBars;
            ESRI.ArcGIS.esriSystem.UID cmdID = new ESRI.ArcGIS.esriSystem.UIDClass();
            cmdID.Value = "esriArcMapUI.SelectTool";
            ICommandItem cmdItem = cbrs.Find(cmdID, false, false);

            if (cmdItem != null)
            {
                ArcMap.Application.CurrentTool = cmdItem;
            }
        }
    }
}
